<template>
  <div>
    <el-card>
      <el-row>
        <el-col :span="6">
          单号：<el-input
            v-model="serachModel.oddNumber"
            placeholder="请输入单号"
            style="width: 300px; margin: 10px"
            clearable
          ></el-input>
        </el-col>
        <el-col :span="6"
          >序列号：<el-input
            v-model="serachModel.serialNumber"
            placeholder="请输入序列号"
            style="width: 300px; margin: 10px"
            clearable
          ></el-input
        ></el-col>
        <el-col :span="6"
          >货物类型：
          <el-select
            v-model="serachModel.type"
            placeholder="请选择"
            style="width: 300px; margin: 10px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option> </el-select
        ></el-col>
        <el-col :span="6">
          日 期：
          <el-date-picker
            v-model="serachModel.createTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            clearable
            style="width: 300px; margin: 10px"
          ></el-date-picker
        ></el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-button type="primary" @click="getGoodsList">查询</el-button>
          <el-button type="primary" @click="GoodsUi(null)">新增</el-button>
        </el-col>
        <el-col :span="4" align="right" style="margin-top: 10px">
          <el-button
            type="primary"
            @click="openTransferUi"
            style="margin-right: 10px"
            >序列号入库</el-button
          >

          <span v-if="goodsRemarkStatus != 0">
            <el-badge
              :value="goodsRemarkStatus"
              class="item"
              style="margin-top: 10px"
            >
              <el-button @click="openSignUi()" type="warning">登记</el-button>
            </el-badge>
          </span>
          <span v-else>
            <el-button @click="openSignUi()">登记</el-button>
          </span>
        </el-col>
      </el-row>
    </el-card>
    <el-card>
      <el-table
        :data="goodsList"
        border
        style="width: 100%"
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50"> </el-table-column>
        <el-table-column type="index" width="50" align="center">
        </el-table-column>

        <el-table-column
          prop="createTime"
          label="日期"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="oddNumber"
          label="单号"
          width="300"
          align="center"
        >
        </el-table-column>
        <el-table-column label="序列号" width="360" align="center">
          <template slot-scope="scope">
            <div
              v-for="(serialNumber, index) in scope.row.serialNumbers"
              :key="index"
            >
              <div style="margin: 4px">
                <el-tag
                  ><strong
                    >{{ serialNumber.serialNumber }}-<span
                      v-if="serialNumber.status"
                      >已入库</span
                    ><span v-else>待入库</span>
                  </strong></el-tag
                >
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="设备状态" width="180" align="center">
          <template slot-scope="scope">
            <span
              v-for="(value, index) in scope.row.info"
              :key="index"
              style="margin: 10px"
            >
              <el-tag type="danger"
                ><strong>{{ value }}</strong></el-tag
              >
            </span>
            <div style="margin-left: 10px; margin-top: 10px">
              <el-tag
                v-if="scope.row.type === '寄回设备'"
                effect="dark"
                type="success"
                >{{ scope.row.type }}</el-tag
              >
              <el-tag v-if="scope.row.type === '采购设备'" effect="dark">{{
                scope.row.type
              }}</el-tag>
              <el-tag
                v-if="scope.row.type === '配件'"
                effect="dark"
                type="info"
                >{{ scope.row.type }}</el-tag
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作人" align="center" width="120">
          <template slot-scope="scope">
            <el-tag effect="dark">{{ scope.row.operator }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="GoodsUi(scope.row)"
                :loading="loading"
              ></el-button>
              <el-button
                type="primary"
                icon="el-icon-delete"
                @click="deleteById(scope.row)"
                :loading="loading"
              ></el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="serachModel.pageNo"
      :page-sizes="[20, 40, 60, 80]"
      :page-size="serachModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增和修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="cleaForm"
      :before-close="handleClose"
    >
      <span style="margin-left: 10px"
        >操作提示：F8保存收货信息，F9打开收货对话框</span
      >
      <el-divider></el-divider>
      <el-form
        :model="goodsForm"
        class="demo-form-inline"
        ref="GoodsFormRef"
        :rules="rules"
      >
        <el-form-item label="货物状态" :label-width="formLabelWidth">
          <el-checkbox-group v-model="goodsForm.info">
            <el-checkbox label="缺配件" name="type"></el-checkbox>
            <el-checkbox label="损坏" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item
          label="运单号"
          prop="oddNumber"
          :label-width="formLabelWidth"
        >
          <!-- v-model="goodsForm.oddNumber" -->

          <el-input
            v-model="goodsForm.oddNumber"
            autocomplete="off"
            placeholder="运单号"
            ref="oddNumber"
            @keyup.native.enter="foucusNext('serialNumver')"
            @blur="isSignFlag"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="序列号"
          prop="serialNumber"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="goodsForm.serialNumber"
            autocomplete="off"
            placeholder="序列号"
            ref="serialNumver"
            @keyup.native.enter="foucusNext('remark')"
          ></el-input>
        </el-form-item>

        <el-form-item label="备 注" prop="remark" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="goodsForm.remark"
            ref="remark"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="货物类型" :label-width="formLabelWidth">
          <el-select v-model="goodsForm.type" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveGoods" :loading="loading"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-drawer
      title="登记订单备注"
      :visible.sync="remarkVisible"
      direction="rtl"
      size="50%"
    >
      <el-card>
        <el-row
          ><el-col :span="24" align="right" style="margin: 10px">
            <el-button @click="remarkUi(null)">新增</el-button></el-col
          ></el-row
        >
        <el-row>
          <el-col :span="24">
            <el-table :data="remarkData">
              <el-table-column property="number" label="单号" width="150">
              </el-table-column>
              <el-table-column property="remark" label="备注"></el-table-column>
              <el-table-column label="状态">
                <template slot-scope="scope">
                  <el-tag
                    effect="dark"
                    type="warning"
                    v-if="scope.row.status == false"
                  >
                    未处理</el-tag
                  >
                  <el-tag effect="dark" v-else>已处理</el-tag>
                </template>
              </el-table-column>

              <el-table-column
                property="createTime"
                label="创建时间"
              ></el-table-column>

              <el-table-column label="操作" width="280">
                <template slot-scope="scope">
                  <el-button
                    type="primary"
                    v-if="scope.row.status == false"
                    size="mini"
                    @click="updateSignStatus(scope.row.id)"
                    >已处理</el-button
                  >
                  <el-button
                    type="primary"
                    size="mini"
                    @click="updateSignStatus(scope.row.id)"
                    v-else
                    >未处理</el-button
                  >
                  <el-button @click="remarkUi(scope.row)" size="mini"
                    >编辑</el-button
                  >
                  <el-button @click="DeleteRemarkById(scope.row)" size="mini"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-card>
    </el-drawer>
    <!-- 运单登记对话框 -->
    <el-dialog
      :title="remarkTitle"
      :visible.sync="dialogFormVisibleRemark"
      width="30%"
      @close="clearSignForm"
      :before-close="handleClose"
    >
      <el-form
        :model="remarkForm"
        class="demo-form-inline"
        ref="RemarkFormRef"
        :rules="remarkRules"
      >
        <el-form-item label="单号" :label-width="formLabelWidth" prop="number">
          <el-input v-model="remarkForm.number" placeholder="运单号"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="remarkForm.remark" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleRemark = false">取 消</el-button>
        <el-button type="primary" @click="saveRemark" :loading="loading"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 序列号出库对话框 -->
    <el-dialog
      title="序列号入库"
      :visible.sync="dialogVisibleLogOutWarehouse"
      width="60%"
      :before-close="handleClose"
      @close="clearTransferData"
    >
      <div align="center">
        <el-row>
          <el-col :span="12">
            调出仓库：
            <el-select
              v-model="transGoodsForm.outWarehouseId"
              filterable
              placeholder="调出仓库"
              style="margin: 10px"
              @change="getInWarehouseNum"
              clearable
            >
              <el-option
                v-for="item in allwareHouseName"
                :key="item.id"
                :label="item.warehouseName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="12">
            调入仓库：
            <el-select
              v-model="transGoodsForm.saveWarehouseId"
              filterable
              placeholder="调入仓库"
              style="margin: 10px"
              clearable
            >
              <el-option
                v-for="item in allwareHouseName"
                :key="item.id"
                :label="item.warehouseName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-tabs
            type="border-card"
            v-model="activeName"
            v-loading="transferLoading"
          >
            <el-tab-pane :label="getTableName('在库')" name="first">
              <el-table :data="transGoodsForm.inWarehouseSerialNumber">
                <el-table-column prop="serialNumber" label="序列号">
                </el-table-column>
                <el-table-column
                  prop="productName"
                  label="商品名称"
                ></el-table-column>
                <el-table-column
                  prop="warehouseName"
                  label="所在仓库"
                ></el-table-column>
              </el-table>
            </el-tab-pane>
            <el-tab-pane :label="getTableName('不在库')" name="second">
              <el-table :data="transGoodsForm.outWarehouseSerialNumber">
                <el-table-column
                  prop="serialNumber"
                  label="序列号"
                ></el-table-column>
                <el-table-column
                  prop="productName"
                  label="商品名称"
                ></el-table-column>
                <el-table-column
                  prop="warehouseName"
                  label="所在仓库"
                ></el-table-column>

                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button
                      type="primary"
                      size="mini"
                      @click="updateSerialNumberStatus(scope.row)"
                      >已入库</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </el-row>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleLogOutWarehouse = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="transferSerialNumber"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import goodsApi from "@/api/goods";
import goodsRemarkApi from "@/api/goodsRemark";
import serivalNumberApi from "@/api/SerialNumber";
import warehouseApi from "@/api/inventory/warehouse";
import inventoryApi from "@/api/inventory/inventory";

export default {
  data() {
    return {
      // 序列号入库
      // 查询序列号状态数据
      transferData: {
        serialNumbers: [],
      },
      multipleSelection: [],
      dialogVisibleLogOutWarehouse: false,
      transGoodsForm: {
        inWarehouseSerialNumber: [],
        outWarehouseSerialNumber: [],
      },
      transferLoading: false,

      isWarehouseName: "在库(0)",
      outWarehouseName: "不在库(0)",

      activeName: "first",

      // 仓库名称
      allwareHouseName: [],

      options: [
        {
          value: "寄回设备",
          label: "寄回设备",
        },
        {
          value: "采购设备",
          label: "采购设备",
        },
        {
          value: "配件",
          label: "配件",
        },
      ],

      remarkTitle: "",
      remarkForm: {},
      remarkData: [],

      remarkVisible: false,
      goodsRemarkStatus: 0,
      title: "",
      total: 0,
      serachModel: {
        pageNo: 1,
        pageSize: 10,
        createTime: new Date(),
      },
      goodsForm: {
        type: "寄回设备",
        info: [],
      },

      goodsList: [],

      dialogFormVisible: false,

      dialogFormVisibleRemark: false,

      loading: false,

      formLabelWidth: "130px",

      rules: {
        oddNumber: [
          { required: true, message: "请输入运单号", trigger: "blur" },
          {
            min: 10,
            max: 50,
            message: "长度在 10 到 50 个字符",
            trigger: "blur",
          },
        ],
        serialNumber: [
          {
            min: 5,
            max: 50,
            message: "长度在 5 到 50 个字符",
            trigger: "blur",
          },
        ],
        remark: [
          {
            min: 1,
            max: 255,
            message: "长度在 1 到 255 个字符",
            trigger: "blur",
          },
        ],
      },

      remarkRules: {
        number: [
          { required: true, message: "请输入运单号", trigger: "blur" },
          {
            min: 10,
            max: 50,
            message: "长度在 10 到 50 个字符",
            trigger: "blur",
          },
        ],
        remark: [
          {
            min: 1,
            max: 255,
            message: "长度在 1 到 255 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 确认关闭对话框
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 清空库存调拨数据
    clearTransferData() {
      this.transferData = {
        serialNumbers: [],
      };

      this.transGoodsForm = {
        inWarehouseSerialNumber: [],
        outWarehouseSerialNumber: [],
      };

      this.transferLoading = false;

      this.isWarehouseName = "在库(0)";
      this.outWarehouseName = "不在库(0)";

      this.activeName = "first";
    },

    // 清理表单数据
    cleaForm() {
      // 清理数据
      this.goodsForm = {
        type: "寄回设备",
        info: [],
      };

      if (
        this.$refs.GoodsFormRef != null &&
        this.$refs.GoodsFormRef != undefined
      ) {
        this.$refs.GoodsFormRef.clearValidate();
      }
    },

    clearSignForm() {
      this.remarkForm = {};
      if (
        this.$refs.RemarkFormRef != null &&
        this.$refs.RemarkFormRef != undefined
      ) {
        this.$refs.RemarkFormRef.clearValidate();
      }
    },

    handleSizeChange(pageSize) {
      this.serachModel.pageSize = pageSize;
      this.getGoodsList();
    },
    handleCurrentChange(pageNo) {
      this.serachModel.pageNo = pageNo;
      this.getGoodsList();
    },

    getGoodsList() {
      this.loading = true;
      // 处理默认值

      goodsApi.getReceiveGoodsList(this.serachModel).then((response) => {
        this.goodsList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
        this.getRemarkList();
      });
    },

    formatDateTime(dateString) {
      const date = new Date(dateString);

      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      const seconds = String(date.getSeconds()).padStart(2, "0");

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },

    getRemarkList() {
      goodsRemarkApi.getGoodsRemarkList().then((response) => {
        this.remarkData = response.data;
      });
    },

    saveGoods() {
      this.$refs.GoodsFormRef.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.goodsForm.info != null || this.goodsForm.info != undefined) {
            if (this.goodsForm.info.length > 0) {
              var info = "";
              for (const i of this.goodsForm.info) {
                info += i;
              }
              this.goodsForm.info = info;
            } else {
              this.goodsForm.info = "";
            }
          }

          // 提交数据给后台
          goodsApi.saveReceiveGoods(this.goodsForm).then((response) => {
            if (response.code == 20000) {
              this.$message({
                showClose: true,
                message: response.message,
                type: "success",
              });
              // 关闭对话框
              this.dialogFormVisible = false;
              // 刷新表单
              this.getGoodsList();
              this.loading = false;
            } else {
              this.$message({
                showClose: true,
                message: response.message,
                type: "error",
              });
              this.loading = false;
            }
          });
        } else {
          // 验证不通过
          this.$message({
            showClose: true,
            message: "表单验证不通过",
            type: "error",
          });
          this.loading = false;
          return false;
        }
      });
    },

    // 显示序列号处理UI
    GoodsUi(goods) {
      if (goods == null || goods == undefined) {
        this.title = "新增收货记录";
        this.dialogFormVisible = true;
        // 定位光标
        this.$nextTick(() => {
          if (this.$refs["oddNumber"]) {
            this.$refs["oddNumber"].focus();
          }
        });
      } else {
        this.title = "修改收货记录";
        this.loading = true;
        goodsApi.getReceiveGoodsById(goods.id).then((response) => {
          // this.goodsForm = response.data;
          this.goodsForm = response.data;

          this.loading = false;
        });

        this.loading = false;
        this.dialogFormVisible = true;
      }
    },

    deleteById(goods) {
      this.$confirm(`您确认该收货记录 ${goods.oddNumber}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          goodsApi.deleteReceiveGoodsById(goods.id).then((response) => {
            this.$message({
              type: "success",
              message: response.message,
            });
            this.getGoodsList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 显示订单处理UI
    remarkUi(remark) {
      if (remark == null || remark == undefined) {
        this.remarkTitle = "新增登记记录";
        this.dialogFormVisibleRemark = true;
      } else {
        this.remarkTitle = "修改登记记录";
        this.loading = true;
        goodsRemarkApi.getGoodsRemarkById(remark.id).then((response) => {
          this.remarkForm = response.data;
          this.loading = false;
        });
        this.loading = false;
        this.dialogFormVisibleRemark = true;
      }
    },

    DeleteRemarkById(remark) {
      this.$confirm(`您确认该用户的删除订单 ${remark.phone}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          goodsRemarkApi.deleteRemarkById(remark.id).then((response) => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getGoodsList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    saveRemark() {
      this.$refs.RemarkFormRef.validate((valid) => {
        if (valid) {
          this.loading = true;
          // 提交数据给后台
          goodsRemarkApi.saveRemark(this.remarkForm).then((response) => {
            if (response.code === 20000) {
              this.$message({
                showClose: true,
                message: response.message,
                type: "success",
              });
              // 关闭对话框
              this.dialogFormVisibleRemark = false;
              // 刷新表单
              this.getGoodsList();
            } else {
              this.$message({
                showClose: true,
                message: response.message,
                type: "error",
              });

              // 刷新表单
              this.getGoodsList();
            }

            this.loading = false;
          });
        } else {
          // 验证不通过
          this.$message({
            showClose: true,
            message: "表单验证不通过",
            type: "error",
          });
          this.loading = false;
          return false;
        }
      });
    },

    foucusNext(nextRef) {
      this.$refs[nextRef].focus();
    },

    // 更新订单状态
    updateSignStatus(id) {
      this.$confirm("确定要更新登记状态吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        goodsRemarkApi.updateStatusSing(id).then((response) => {
          this.$message({
            type: "success",
            message: response.message,
          });

          this.getRemarkList();
        });
      });
    },

    // 打开登记筹集
    openSignUi() {
      this.remarkVisible = true;
      this.getRemarkList();
    },

    // 判断订单是否被登记
    isSignFlag() {
      if (
        this.goodsForm.oddNumber != null &&
        this.goodsForm.oddNumber != undefined &&
        this.goodsForm.oddNumber !== ""
      ) {
        goodsRemarkApi
          .getIsSignFlag(this.goodsForm.oddNumber)
          .then((response) => {
            if (response.code === 20000) {
              this.$confirm(`${response.data.remark}`, "运单号已登记", {
                confirmButtonText: "已处理",
                cancelButtonText: "未处理",
                type: "warning",
              })
                .then(() => {
                  // 更新运单号状态为已处理
                  if (
                    this.goodsForm.remark == undefined ||
                    this.goodsForm == null
                  ) {
                    this.goodsForm.remark = response.data.remark;
                  } else {
                    this.goodsForm.remark += response.data.remark;
                  }

                  goodsRemarkApi
                    .updateStatusSing(response.data.id)
                    .then((resp) => {
                      this.$message({
                        type: "success",
                        message: resp.message,
                      });
                    });
                })
                .catch(() => {
                  if (
                    this.goodsForm.remark == undefined ||
                    this.goodsForm == null
                  ) {
                    this.goodsForm.remark = response.data.remark;
                  } else {
                    this.goodsForm.remark += response.data.remark;
                  }

                  this.$message({
                    type: "info",
                    message: "已将登记信息添加到备注",
                  });
                });
            }
          });
      }
    },

    handleKeyDown(event) {
      if (event.key === "F9") {
        if (this.dialogFormVisible == false) {
          this.GoodsUi(null);
        }
      } else if (event.key === "F8") {
        if (this.dialogFormVisible == true) {
          this.saveGoods();
        }
      }
    },

    // 打开库存调拨对话框
    openTransferUi() {
      // 选中订单绑定的序列号
      var serialNumbers = [];
      this.multipleSelection.forEach((order) => {
        order.serialNumbers.forEach((serialNumber) => {
          serialNumbers.push(serialNumber.serialNumber);
        });
      });

      this.transferData.serialNumbers = serialNumbers;

      // 需要的数据有，{序列号}

      this.dialogVisibleLogOutWarehouse = true;
    },
    handleSelectionChange(val) {
      // 保存选中的对象
      this.multipleSelection = val;
    },

    // 获取序列号状态
    getInWarehouseNum() {
      // 获取序列号状态
      // 返回的数据在库，和不在库两组数据{Inwarehouse:[传递的序列号], outWarehouse:[传递的序列号]}
      this.transferLoading = true;
      this.transferData.warehouseId = this.transGoodsForm.outWarehouseId;

      this.transferData.type = false;
      inventoryApi
        .getInWarehouseSerialNumber(this.transferData)
        .then((response) => {
          if (response.code == 20000) {
            this.transGoodsForm.inWarehouseSerialNumber =
              response.data.inWarehouseSerialNumber;
            this.transGoodsForm.outWarehouseSerialNumber =
              response.data.outWarehouseSerialNumber;
          } else {
            this.$message({ type: "error", message: response.message });
          }

          this.transferLoading = false;
        });
    },

    getTableName(name) {
      if (name === "在库") {
        return (
          name + "(" + this.transGoodsForm.inWarehouseSerialNumber.length + ")"
        );
      } else if (name === "不在库") {
        return (
          name + "(" + this.transGoodsForm.outWarehouseSerialNumber.length + ")"
        );
      }
    },

    // 序列号调拨
    transferSerialNumber() {
      var serialNumbers = [];

      this.transGoodsForm.inWarehouseSerialNumber.forEach((item) => {
        var serialNumber = {};
        serialNumber.receiveGoodsId = item.receiveGoodsId;
        serialNumber.serialNumber = item.serialNumber;
        serialNumbers.push(serialNumber);
      });

      var transfer = {
        outWarehouseId: this.transGoodsForm.outWarehouseId,
        saveWarehouseId: this.transGoodsForm.saveWarehouseId,
        serialNumbers: serialNumbers,
        serialNumberStatus: true, // 已出库
        type: false, // 收货
      };

      // 判断是否能调拨
      if (
        transfer.outWarehouseId == null ||
        transfer.outWarehouseId == undefined
      ) {
        this.$message({
          type: "error",
          message: "请选择调出仓库",
        });
      } else if (
        transfer.saveWarehouseId == null ||
        transfer.saveWarehouseId == undefined
      ) {
        this.$message({
          type: "error",
          message: "请选择调入仓库",
        });
      } else if (
        transfer.serialNumbers == null ||
        transfer.serialNumbers == undefined ||
        transfer.serialNumbers.length == 0
      ) {
        this.$message({
          type: "error",
          message: "序列号列表为空",
        });
      } else {
        this.$confirm(
          "一定要先签收订单后再，再将序列号入库，不然将不更新此序列号对应的订单，导致订单不能签收。",
          "温馨提示",
          {
            confirmButtonText: "我已将签收订单",
            cancelButtonText: "我现在去签收订单",
            type: "warning",
          }
        ).then(() => {
          this.$confirm("此操作会调拨仓库中的商品, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            // 序列号调拨
            serivalNumberApi.TransferSerialNumber(transfer).then((response) => {
              if (response.code === 20000) {
                this.$message({
                  type: "success",
                  message: response.message,
                });

                // 关闭对话框
                this.dialogVisibleLogOutWarehouse = false;

                // 刷新
                this.getGoodsList();
              } else {
                this.$message({
                  type: "error",
                  message: response.message,
                });
              }
            });
          });
        });
      }
    },

    // 获取所有仓库名称
    getAllwareHouseName() {
      warehouseApi.selseAllName().then((response) => {
        this.allwareHouseName = response.data;
      });
    },

    updateSerialNumberStatus(row) {
      this.$confirm(`确定要将该序列号的状态设置为-已出库？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 序列号入库
        var serialNumbersAndStatus = {
          serialNumber: row.serialNumber,
          status: true, //更新为已出库
          type: false, //收货
        };

        serivalNumberApi
          .updateSerialNumber(serialNumbersAndStatus)
          .then((response) => {
            if (response.code === 20000) {
              this.$message({
                type: "success",
                message: response.message,
              });
            } else {
              this.$message({
                type: "warning",
                message: response.message,
              });
            }
            this.getGoodsList();
          });
      });
    },
  },

  mounted() {
    window.addEventListener("keydown", this.handleKeyDown);
  },

  beforeDestroy() {
    window.removeEventListener("keydown", this.handleKeyDown);
  },

  created() {
    this.serachModel.createTime = this.formatDateTime(
      this.serachModel.createTime
    );
    this.getGoodsList();
    this.getAllwareHouseName();
  },
};
</script>

<style scoped></style>
